package com.mybed.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybed.crm.anno.Log;
import com.mybed.crm.pojo.dto.PageCourseDto;
import com.mybed.crm.pojo.pojo.Course;
import com.mybed.crm.pojo.pojo.Dict;
import com.mybed.crm.pojo.vo.CourseVo;
import com.mybed.crm.result.PageResult;
import com.mybed.crm.result.Result;
import com.mybed.crm.result.ResultP;
import com.mybed.crm.service.CourseService;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/clues/course")
@RestController
@Slf4j
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 课程列表分页查询
     */
    @GetMapping("/list")
    public PageResult pageCourseQuery(PageCourseDto dto) {
        log.info("课程列表分页查询---------：{}", dto);
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper();
        wrapper.like(StringUtils.hasLength(dto.getName()), Course::getName, dto.getName());
        wrapper.eq(StringUtils.hasLength(dto.getCode()), Course::getCode, dto.getCode());
        wrapper.eq(StringUtils.hasLength(dto.getSubject()), Course::getSubject, dto.getSubject());
        wrapper.eq(StringUtils.hasLength(dto.getApplicablePerson()), Course::getApplicablePerson, dto.getApplicablePerson());
        if (dto.getParams() != null) {
            wrapper.between(StringUtils.hasLength(dto.getParams().get("beginCreateTime"))
                            && StringUtils.hasLength(dto.getParams().get("endCreateTime"))
                    , Course::getCreateTime, dto.getParams().get("beginCreateTime")
                    , dto.getParams().get("endCreateTime"));
        }
        IPage<Course> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        IPage<Course> iPage = courseService.page(page);
        log.info("课程列表分页查询---------：{}", iPage);
        PageResult result = new PageResult();
        result.insertData(iPage);
        return result;
    }

    /**
     * 新增课程
     */
    @Log("新增课程")
    @PostMapping
    public Result addCourse(@RequestBody Course course) {
        return Result.success(courseService.save(course));
    }

    /**
     * 批量删除课程
     */
    @Log("批量删除课程")
    @DeleteMapping("/{ids}")
    public ResultP deleteCourse(@PathVariable String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        courseService.removeByIds(list);
        return ResultP.success();
    }

    /**
     * 查询课程详情
     */
    @GetMapping("/{id}")
    public Result<CourseVo> selectCourseById(@PathVariable("id") Long id) {
        Course course = courseService.getById(id);
        CourseVo courseVo = new CourseVo();
        BeanUtils.copyProperties(course, courseVo);
        return Result.success(courseVo);
    }

    /**
     * 修改课程
     */
    @Log("修改课程")
    @PutMapping
    public Result updateCourse(@RequestBody Course course) {
        courseService.updateById(course);
        return Result.success();
    }

    @ApiModelProperty(value = "根据学科id查询课程")
    @GetMapping("/listselect")
    public Result<List<Course>> selectCoursesById(Long subject) {
        if (subject!=null) {
            QueryWrapper<Course> wrapper = new QueryWrapper<>();
            wrapper.eq("subject", subject);
            List<Course> list = courseService.list(wrapper);
            log.info("查询课程列表：{}", list);
            return Result.success(list);
        } else {
            List<Course> list = courseService.list();
            return Result.success(list);
        }
    }

}
